﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Portal.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Globalization;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Utilities;
using ESF.SharePoint.Domain;
using Microsoft.Office.Server.ActivityFeed;
using System.Collections.Generic;
using ESF.SharePoint.Utility;
using System.Reflection;

namespace ESF.SharePoint.WebParts
{
    [ToolboxItemAttribute(false)]
    public class BasicActivityAggregatorWebPart : NewsFeedWebPartBase
    {
        // Field
        private int _maxItemsToDisplay = 10;
        //private UserProfile _pageProfile;
        private const int maxItemsDefaultValue = 10;

        // Methods
        protected override void OnPreRender(object sender, EventArgs e)
        {
            base.ShowUserPicture = true;
            using (new SPMonitoredScope("ActivityAggregatorWebPart.OnPreRender"))
            {
                SocialFeeds sf = new SocialFeeds();
                base.m_EventsCollection = sf.GetActivitiesFromMyNetwork();

                base.OnPreRender(e);
            }
        }

        protected override void RenderWebPart(HtmlTextWriter writer)
        {
            if ((base.m_EventsCollection == null) || (base.m_EventsCollection.Count <= 0))
            {
                string format = "No feed was found.";//StringResourceManager.GetString(LocStringId.RecentActivities_NoResults_Text);
                //format = string.Format(CultureInfo.CurrentCulture, format, new object[] { (string) this.m_pageProfile["PreferredName"].Value });
                writer.Write("<div style=\"padding: 5px 0px\">" + SPHttpUtility.HtmlEncode(format) + "</div>");
            }
            else
            {
                base.RenderWebPart(writer);
            }
        }

        // Properties
        [Resources("QueryResults_MaxItem_PropertyName", "Webpart__Custome_Property_Text", "QueryResults_MaxItem_PropertyDescription"), WebPartStorage(Storage.Shared), DefaultValue(10)]
        public override int MaxItemsToDisplay
        {
            get
            {
                return this._maxItemsToDisplay;
            }
            set
            {
                if (value < 0)
                {
                    this._maxItemsToDisplay = 0;
                }
                else if (value > 100)
                {
                    this._maxItemsToDisplay = 100;
                }
                else
                {
                    this._maxItemsToDisplay = value;
                }
            }
        }
    }
}
